Add safe volatile functions.#47975
Conversation
|
(rust_highfive has picked a reviewer for you, use r? to override) |
eeb07a6 to
81045f2
Compare
81045f2 to
2c09c2f
Compare
|
I'm not sure we should land these without first establishing a more formal memory model. In particular, afaict a types-as-contracts-based interpretation would make |
|
@cramerj I see |
|
@clarcharr Cell and other !Freeze types are a special exception to this rule that the compiler understands. |
|
I share the doubts about how useful a volatile load through a safe reference is. I don't believe it's likely to be miscompiled -- in practice LLVM will probably let I also have some doubts about the robustness of the functions that are intended to partially replace |
|
In terms of review I'd personally like to see these functions used to see whether in practice this works or not first as well. |
|
It feels like there's a moderate amount of response to not accept this PR; am I reading the comments correct? Is the team leaning towards closing this PR, perhaps in favor of discussion on IRLO? |
|
@cramertj @alexcrichton @rkruppe Should this PR be closed in favor of an RFC? The author has offered to draft one. |
|
Yeah let's close this for now, I think this may want to have more discussion on an RFC or perhaps on internals before we add to libstd. In the meantime as well this can certainly be added as a crate on crates.io! |
This adds safe equivalents of
read_volatileandwrite_volatiletostd::mem, using references instead of pointers.The
move_opaqueanddrop_opaquefunctions are thevalue_fenceandcompute_and_dropmentioned in rust-lang/rfcs#1484 (comment).move_opaqueis roughly equivalent to the unstabletest::black_box.copy_opaqueandreplace_opaqueare added for completeness, as they are useful equivalentsread_volatileandwrite_volatilethat will work in most cases. This means that volatile reads and writes don't have to be annotated withunsafeon each occurrence, and that the programmer simply has to verify that the pointers are correct and cast them to references (probably with'staticlifetime).If an RFC is required to add these, I'd be willing to write one. However, I think they're reasonable enough to have a PR for now.